using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public class InsuranceBll
{       
    DBAccess db = new DBAccess();
    
    public InsuranceBll()
    {

    }
    
    public DataTable getAllInsurance()
    {
        return db.executeSelect("GetAllInsurance");
    }

    public Insurance getInsuranceById(int insuranceId)
    {
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@InsuranceId", SqlDbType.Int);
        para[0].Value = insuranceId;
        DataTable dt = db.executeSelect("GetInsuranceById", para);
        Insurance  insurance = new  Insurance();
        insurance.InsuranceId = int.Parse(dt.Rows[0][0].ToString());
        insurance.InsuranceType = dt.Rows[0][1].ToString();
        insurance.Description = dt.Rows[0][2].ToString();         
        return  insurance;
    }

    public int getInsuranceIdByInsuranceType(string insuranceType)
    {
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@InsuranceType", SqlDbType.NVarChar);
        para[0].Value = insuranceType;
        DataTable dt = db.executeSelect("GetInsuranceIdByInsuranceType", para);
        int insuranceId = int.Parse(dt.Rows[0][0].ToString());
        return insuranceId;
    }

    public bool insertInsurance(Insurance insu)
    {
        bool result;
        SqlParameter[] para =new SqlParameter[2];
        para[0] = new SqlParameter("@InsuranceType", SqlDbType.NVarChar);
        para[0].Value = insu.InsuranceType;
        para[1] = new SqlParameter("@Description", SqlDbType.NText);
        para[1].Value = insu.Description;
        if (db.execute("InsertInsurance", para) > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }

    public bool updateInsuranceById(Insurance insu)
    {
        bool result;
        SqlParameter[] para = new SqlParameter[3];
        para[0] = new SqlParameter("@InsuranceID", SqlDbType.Int);
        para[0].Value = insu.InsuranceId;
        para[1] = new SqlParameter("@InsuranceType", SqlDbType.NVarChar);
        para[1].Value = insu.InsuranceType;
        para[2] = new SqlParameter("@Description", SqlDbType.NText);
        para[2].Value = insu.Description;
        if (db.execute("UpdateInsuranceById", para) > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }

    public bool deleteInsuranceById(int insuranceId)
    {
        bool result;
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@InsuranceId", SqlDbType.Int);
        para[0].Value = insuranceId;
        if (db.execute("DeleteInsuranceById", para) > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }
}
